﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockAnalyzer.HaoStock
{
    /// <summary>
    /// 交易记录，用于记录每一次买入和卖出记录。
    /// </summary>
    public class TradeRecord
    {
        public DateTime buy_date;
        public double buy_price = 0;
        public double buy_amount = 0;

        public DateTime sell_date;
        public double sell_price = 0;
        public double sell_amount = 0;

        public OperationState state = OperationState.Unstarted;

        private DateTime date0 = new DateTime(2020, 1, 1);


        public void Initialize()
        {
            state = OperationState.Unstarted;

            buy_date = date0;
            buy_amount = 0;
            buy_price = 0;

            sell_date = date0;
            sell_price = 0;
            sell_amount = 0;
        }

        public TradeRecord()
        {

        }

        public TradeRecord(DateTime date, double price, double amount)
        {
            Initialize();
            this.buy_date = date;
            this.buy_price = price;
            this.buy_amount = amount;
        }




        public override string ToString()
        {
            double interest = (sell_price - buy_price) * buy_amount;

            StringBuilder sb = new StringBuilder();
            sb.Append($"{buy_date:yyyy-MM-dd} {buy_price,6:N3} {buy_amount,5:0}");
            if (state == OperationState.Sold)
            {
                sb.Append($" --({(sell_date - buy_date).TotalDays:000})--> ");
                sb.Append($"{sell_date:yyyy-MM-dd} {sell_price,6:N3} {sell_amount,5:0} ");
                sb.Append($"{interest,6:N0} {state}");
            }
            return sb.ToString();
        }



        /// <summary>
        /// 对全部7个变量进行复制。
        /// </summary>
        /// <returns></returns>
        public TradeRecord clone()
        {
            return new TradeRecord()
            {
                state = this.state,

                buy_date = this.buy_date,
                buy_price = this.buy_price,
                buy_amount = this.buy_amount,

                sell_date = this.sell_date,
                sell_price = this.sell_price,
                sell_amount = this.sell_amount,
            };
        }




        /// <summary>
        /// 保留目标数量和买入价格，其他值清零。
        /// </summary>
        public void reset()
        {
            state = OperationState.Unstarted;
            buy_date = date0;
            buy_price = 0;
            buy_amount = 0;
            sell_date = date0;
            sell_price = 0;
            sell_amount = 0;
        }

        public double sell(DateTime date, double price)
        {
            state = OperationState.Sold;
            sell_date = date;
            sell_price = price;
            sell_amount = buy_amount;
            return sell_price * sell_amount;
        }

        public double buy(DateTime date, double price, double amount)
        {
            state = OperationState.Bought;
            buy_date = date;
            buy_price = price;
            buy_amount = amount;
            return buy_price * buy_amount;
        }
    }
}
